<template>
  <div>
    <el-dialog title="编辑区域" :visible.sync="dialogFormVisible2">
      <el-form :model="search">
        <el-form-item label="编码" prop="id">
          <el-input v-model="search.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="search.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="editArea()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// 导入添加菜单api
import { EditArea } from "@/api/shop";
export default {
  props: {
    // 控制是否显示弹框
    visible: { type: Boolean, default: false },
    id: {
      type: Object,
      default() {
        return {};
      },
    },
  },

  data() {
    return {
      // 控制组件是否显示弹框
      dialogFormVisible2: this.visible,
      // 被添加的menu
      search: {},
    };
  },
  methods: {
    editArea() {
      EditArea(this.search).then((res) => {
        if (res.data.code === 0) {
          this.$message({
            type: "success",
            message: "编辑成功",
          });
         
        }
        // 通知父组件添加成功
        this.$emit("editSuccess", true);
        // this.$parent.getMenu()//不推荐
        // 关闭当前组件
        // 关闭弹框
        this.dialogFormVisible2 = false;
      });
    },
  },
  watch: {
    dialogFormVisible2: {
      handler() {
        // 监听dialogVisible变化，告诉父组件更新visible属性值
        this.$emit("update:visible", this.dialogFormVisible2);
      },
      deep: true,
    },
    visible: {
      handler() {
        // 当复制的visible发生变化时候更新当前组件的
        this.dialogFormVisible2 = this.visible;
      },
    },
    id: {
      handler() {
        // 当复制的visible发生变化时候更新当前组件的
        this.search = {...this.id};
      },
    },
  },
};
</script>
